package com.packagetracker.views.overlays;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import android.app.AlertDialog;
import android.graphics.drawable.Drawable;

import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.OverlayItem;
import com.packagetracker.R;
import com.packagetracker.adapters.PackageActivityAdapter;
import com.packagetracker.carriers.PackageLocation;
import com.packagetracker.views.TrackerMap;

public class TrackerOverlay extends ItemizedOverlay<OverlayItem> {
	
	private TrackerMap tracker;
	private LinkedList<PackageLocation> locations;
	private List<OverlayItem> items = new ArrayList<OverlayItem>();
	
	public TrackerOverlay(final TrackerMap tracker, final Drawable marker, final LinkedList<PackageLocation> locations) {
		super(marker);
		
		this.tracker = tracker;
		this.locations = locations;
		
		for (final PackageLocation location : locations) {
			this.items.add(new OverlayItem(location.getGeoPoint(), null, null));
		}
		
		ItemizedOverlay.boundCenterBottom(marker);
		
		this.populate();
	}
	
	@Override
	protected OverlayItem createItem(final int location) {
		return this.items.get(location);
	}
	
	@Override
	protected boolean onTap(final int location) {
		new AlertDialog.Builder(this.tracker)
			.setTitle(this.locations.get(location).toString())
			.setAdapter(new PackageActivityAdapter(this.tracker, R.layout.details_dialog, this.locations.get(location).getDetails()), null)
			.create()
			.show();
			
		return true;
	}
	
	@Override
	public int size() {
		return this.items.size();
	}
	
}